﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Windows.Media.Media3D;

namespace Ascend
{
	/// <summary>
	/// An Ascend.SceneNode that contains a System.Windows.Media.Media3D.GeometryModel3D. This class
	/// cannot be inherited externally.
	/// </summary>
	public class ModelSceneNode : SceneNode
	{
		#region Lifecycle
		internal ModelSceneNode()
		{
			// Create default model content
			Data.Children.Add(DEFAULT_MODEL);
		}

		/// <summary>
		/// Creates a new instance of the Ascend.ModelSceneNode class.
		/// </summary>
		public static ModelSceneNode Create()
		{
			return new ModelSceneNode();
		}

		/// <summary>
		/// Creates a new instance of the Ascend.ModelSceneNode class with given model content.
		/// </summary>
		/// <param name="model">
		/// The model content for the Ascend.ModelSceneNode.
		/// </param>
		public static ModelSceneNode Create(GeometryModel3D model)
		{
			return new ModelSceneNode
			{
				Model = model
			};
		}
		#endregion

		#region Constants
		private readonly GeometryModel3D DEFAULT_MODEL = new GeometryModel3D();
		#endregion

		#region Events
		/// <summary>
		/// Occurs when a mouse moves onto an Ascend.ModelSceneNode.
		/// </summary>
		public event SceneNodeMouseEventHandler MouseEnter;

		/// <summary>
		/// Occurs when a mouse moves off of an Ascend.ModelSceneNode.
		/// </summary>
		public event SceneNodeMouseEventHandler MouseLeave;

		#endregion

		#region Private Members
		private bool _isMouseOver = false;
		#endregion

		#region Internal Members
		internal List<Point3D> _originalVertexPositions;
		internal List<Vector3D> _originalNormals;
		#endregion

		#region Public Properties
		private GeometryModel3D _model;
		/// <summary>
		/// Gets or sets the model content of this Ascend.ModelSceneNode.
		/// </summary>
		public GeometryModel3D Model
		{
			get
			{
				return _model;
			}

			set
			{
				if (_model == value)
				{
					return;
				}

				if (value != null && value.Geometry != null &&
					value.Geometry is MeshGeometry3D == false)
				{
					throw new ArgumentException(
						"The value's geometry must be either null or of type " + 
						"System.Windows.Media.Media3D.MeshGeometry3D", "value");
				}

				_model = value;

				Data.Children[1] = (_model != null)
					? _model
					: DEFAULT_MODEL;

				if (_model != null && _model.Geometry != null)
				{
					_originalVertexPositions =
						new List<Point3D>(((MeshGeometry3D)_model.Geometry).Positions);
					_originalNormals =
						new List<Vector3D>(((MeshGeometry3D)_model.Geometry).Normals);
				}
			}
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Creates a deep copy of this Ascend.ModelSceneNode, including deep copies of all
		/// children.
		/// </summary>
		/// <returns>
		/// Returns the deep copy.
		/// </returns>
		public override TransformableObject Clone()
		{
			ModelSceneNode clone = new ModelSceneNode();

			clone.Model = (Model != null)
				? Model.Clone()
				: null;

			CloneProperties(clone);
			CloneChildren(clone);

			return clone;
		}
		#endregion

		#region Internal Methods
		internal void SetMouseOverState(bool isMouseOver, SceneNodeMouseEventArgs e)
		{
			if (_isMouseOver == isMouseOver)
			{
				return;
			}

			_isMouseOver = isMouseOver;

			// Check if mouse entered or left
			if (isMouseOver)
			{
				RaiseMouseEnterEvent(e);
			}
			else
			{
				RaiseMouseLeaveEvent(e);
			}
		}
		#endregion

		#region Protected Methods
		protected override void CloneProperties(SceneNode clone)
		{
			base.CloneProperties(clone);

			((ModelSceneNode)clone).Model = (Model != null)
				? Model.Clone()
				: null;
		}
		#endregion

		#region Event Invokers
		#region MouseEnter
		private void RaiseMouseEnterEvent(SceneNodeMouseEventArgs e)
		{
			if (MouseEnter != null)
			{
				foreach (Delegate del in MouseEnter.GetInvocationList())
				{
					if (e.Handled)
					{
						return;
					}

					del.Method.Invoke(del.Target, new object[] { this, e });
				}
			}
		}
		#endregion

		#region MouseLeave
		private void RaiseMouseLeaveEvent(SceneNodeMouseEventArgs e)
		{
			if (MouseLeave != null)
			{
				foreach (Delegate del in MouseLeave.GetInvocationList())
				{
					if (e.Handled)
					{
						return;
					}

					del.Method.Invoke(del.Target, new object[] { this, e });
				}
			}
		}
		#endregion
		#endregion
	}
}
